package com.chat.http.validator;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.chat.vo.params.GroupParametersVo;

/**
 * 群聊管理校验器
 * 
 * @author lwh
 *
 */
@Component
public class GroupManageVoValidator implements Validator {
	private static final Logger logger = Logger.getLogger(GroupManageVoValidator.class);
	@Override
	public boolean supports(Class<?> clazz) {
		return GroupParametersVo.class.isAssignableFrom(clazz);
	}

	@Override
	public void validate(Object target, Errors errors) {
	}

	/**
	 * 校验群创建controller<br>
	 * 
	 * @param target
	 *            { userName，token，inviteUsers，groupName }
	 * @param errors
	 */
	public void createGroupValidate(GroupParametersVo target, Errors errors) {
		logger.info("进入建群参数校验");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userName", "用户名不能为空");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "token", "token不能为空");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "groupName", "群名不能为空");
		
		String[] inviteUsers = target.getInviteUsers();
		logger.info("邀请入群用户数量 " + inviteUsers.length + " 个");
		//邀请入群用户数量不得少于2人
		if( inviteUsers == null || ( inviteUsers != null && inviteUsers.length < 2) ){
			errors.reject("邀请入群用户数量不得少于2人");
		}
		
	}

}
